package com.SeleniumTestsGoogleChrome;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.regex.Pattern;
import static org.junit.Assert.*;

public class MakeReservationTestCases extends SeleneseTestBase{
	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}
	
	// Created by Steven Chow
	// Scenario 1: Successful Reservation
	// Test Case 1: User fills out all information
	@Test
	public void testMakeReservationSC1TC1() throws Exception {
		// Go to website
		selenium.open("");
		// Login
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertEquals("Customer Portal", selenium.getTitle());
		// Search for a Flight
		selenium.click("css=div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Search without using the filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Pick the 1st one
		selenium.click("name=r1");
		// Reserve
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in name, seat and choose meal
		selenium.type("name=firstName", "Blue");
		selenium.type("name=lastName", "Bear");
		selenium.type("name=seatID", "F19A");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		// Proceed to Payment
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the payment page
		assertTrue(selenium.isTextPresent("Payment"));
		// Use credit Card for all reservation tests
		selenium.click("name=credit");
		// Enter in correct value
		selenium.type("name=credit", "100");
		// Make the payment
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are back in the Customer portal page
		assertEquals("Customer Portal", selenium.getTitle());
		// Check if there is a message saying that a reservation has been added.
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
	}

	// Created by Steven Chow
	// Scenario 1: Successful Reservation
	// Test Case 2: Check if Seat is taken
	@Test
	public void testMakeReservationSC1TC2() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login
		// Pick the 1st one

		selenium.click("css=div.page-region");
		selenium.type("name=username", "User2");
		selenium.type("name=password", "pass2");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Search for a Flight
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Search without using the filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		// Reserve
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Fill in information
		selenium.type("name=firstName", "Brown");
		selenium.type("name=lastName", "Bear");
		// Check if seat it taken
		selenium.type("name=seatID", "F19A");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		// Change the seat
		selenium.type("name=seatID", "F21A");
		// Proceed to payment
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertTrue(selenium.isTextPresent("Payment"));
		// Pay by credit card for now
		selenium.click("name=credit");
		selenium.type("name=credit", "100");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we are back in the Customer portal page
		assertEquals("Customer Portal", selenium.getTitle());
		// Check if there is a message saying that a reservation has been added.
		assertEquals("1 new reservation added!",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/h4"));
	}

	// Created by Steven Chow
	// Scenario 1: Successful Reservation
	// Test Case 3: Check if total Seat Count Decreased
	@Test
	public void testMakeReservationSC1TC3() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		// Go search for the flight you have made a reservation
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if the #of seats have decreased.
		assertEquals(
				"1000 2012-10-09 13:00 Air Canada Vancouver Los Angeles 200.0 121",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
	}

	// Created by Steven Chow
	// Scenario 1: Successful Reservation
	// Test Case 4: User is able to View their Reservation
	@Test
	public void testMakeReservationSC1TC4() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if the View Reseravation button is there
		assertTrue(selenium
				.isElementPresent("css=div.tile.bg-color-blue > div.tile-content.icon"));
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on te reservation List page
		assertEquals("Reservation List", selenium.getTitle());
		// Check if the Reservation is there
		// Check for airline
		assertEquals(
				"",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/form/table/tbody/tr/td[2]/img"));
		// Check for other stuff
		assertEquals(
				"Blue Bear",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/form/table/tbody/tr/td[5]"));
		assertEquals(
				"100.0",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/form/table/tbody/tr/td[6]"));
		selenium.click("name=r1");
		selenium.click("name=view");
		selenium.waitForPageToLoad("30000");
		// Are we in the Reservation Summary Page
		assertTrue(selenium.isTextPresent("Reservation Summary"));
		// Is that table right?
		assertEquals(
				"Blue Bear F19A Economy Chicken",
				selenium.getText("//html/body/div/div[2]/div/div[2]/div/div/table/tbody/tr/"));
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 1: User is not logged in
	@Test
	public void testMakeReservationSC2TC1() throws Exception {
		// Go to website
		selenium.open("");
		// Is that Search/Book Flight table there?
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		selenium.click("css=div.tile-group");
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Search without filtering
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Reserve the 1st flight
		selenium.click("name=r1");
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get an error message
		assertTrue(selenium
				.isTextPresent("You must sign in to reserve a flight."));
		selenium.click("//a[contains(@href, '/Website/flight/search_flight.jsp?username=null')]");
		selenium.waitForPageToLoad("30000");
		selenium.click("//a[contains(@href, '../index.html')]");
		selenium.waitForPageToLoad("30000");
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 2: User did not Select Meal
	@Test
	public void testMakeReservationSC2TC2() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Are we in the Customer portal Page?
		assertEquals("Customer Portal", selenium.getTitle());
		// Search for a Flight
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Search without filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Select the 1st one
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// Reservation that bitch!
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=firstName", "Green");
		selenium.type("name=lastName", "Bear");
		selenium.type("name=seatID", "F10B");
		// Let's not fill in Meal and see what happens
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// We should be getting an error
		assertTrue(selenium.isTextPresent("Reservation Failed"));
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 3: User did not fill in their 1st and last Name and Seat
	@Test
	public void testMakeReservationSC2TC3() throws Exception {

		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// Login
		selenium.type("name=username", "User2");
		selenium.type("name=password", "pass2");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Are we in the Customer portal page? Tested this like 10000 times
		// already
		assertEquals("Customer Portal", selenium.getTitle());
		// Search for the damn flights yada yada
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Search without the stupid filter!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Click the 1st one of course!! So Obvious!!
		selenium.click("name=r1");
		// Let's Reserve this Baby!!
		// Let's add no name to it and not reserve a seat.
		// I WILL LAUGH SO HARD IF I GET AN HTTP ERROR!!! HAHAHAHAHAHAHA
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("css=label.radio > span");
		// Or course we pick a meal here. BUT WHY NO MCDONALDS????????
		selenium.click("name=mealOption");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// CHECK TO SEE IF WE DON'T GET THE GOD DAMN HTTP ERROR!!!!
		assertTrue(selenium.isTextPresent("Reservation Failed!"));
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 4: User did not fill in any information
	@Test
	public void testMakeReservationSC2TC4() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login of course!! DUHHHHH!!!!!!!!!!!!!!!!!!!!!!
		selenium.type("name=username", "User2");
		selenium.type("name=password", "pass2");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Am I in that Customer Portal????? BETTER BE!!!!!!!!!
		assertEquals("Customer Portal", selenium.getTitle());
		// Search for a Flight for the gazillionth time!!
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Seach No Filter no Jutsu!!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Pick the 1st one of couse cuz #1 is tha beat thing ever!!
		selenium.click("name=r1");
		// Let's reserve this big baby!!
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Let's US MAKE OUR GRAND RESERVATION BY BEING A COMPLETE
		// IDIOT BY NOT FILLING IN ANY INFO
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// PLEASE!!!!!!! NO HTTP ERROR!!!!!!! HTTP ERROR IS PEDOBEAR IN
		// DISGUISE!!
		assertTrue(selenium.isTextPresent("Reservation Failed!"));
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 5: User Reserved the same seat as another User
	@Test
	public void testMakeReservationSC2TC5() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Login like your momma tells ya
		selenium.type("name=username", "User2");
		selenium.type("name=password", "pass2");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// CUSTOMER PORTAL! R U THERE???? REALLY!?? R U THERE!!!
		assertEquals("Customer Portal", selenium.getTitle());
		// Search the Flight, you must!! listen to Master Yoda!!
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// No Filters r needed here.
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.click("name=r1");
		// Reserve this baby!!!
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		selenium.type("name=firstName", "Bug");
		selenium.type("name=lastName", "Bug");
		// HAHA LET'S HIJACK A SEAT THAT IS TAKEN!!!
		selenium.type("name=seatID", "F19A");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		// Let's SEE WHAT HAPPENS!! I HOPE NOT AN HTTP ERROR!!!!!
		// OR A BLANK PAGE!!!!!!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertTrue(selenium.isTextPresent("Reservation Failed!!"));
	}

	// Created by Steven Chow
	// Scenario 2: UnSuccessful Reservation
	// Test Case 6: Invalid Name lengths
	@Test
	public void testMakeReservationSC2TC6() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// L + O +G + I + N = LOGIN
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Is that Customer Town Portal there? A bunch of Orcs are chasing me!!
		assertEquals("Customer Portal", selenium.getTitle());
		// I NEED TO SEARCH FOR A FLIGHT!! THOSE GOD DAMN ORCS SHUT DOWN THE
		// PORTAL!!
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// DAMNNIT!! NO TIME FOR FILTERING!! THAT GIANT ORC IS GONNA KILL ME!!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// THE FIRST FLIGHT OF COURSE!!! NO TIME TO LOOK
		selenium.click("name=r1");
		// NO WAIT THERE IS A LEVEL 90 BADDASS PALADIN TO THE RESCUE
		// DAMN SHE'S ONE HOT BABE!!! KICK THE HORDE'S ASS
		// WAIT WAIT I'm getting side tracked here!! Let's just make a
		// reservation!
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// My First name is sometime that is super long.
		selenium.type("name=firstName",
				"HAHAHAHAHAHAHALJFLADSFAKSDLFKLADFKSDFADFSFADFADSASDdfsdfadsfasddfdfa");
		selenium.type("name=lastName",
				"adfadsfdafhejrklhkalejhsfjsdkahshfweuaeukshfksafadfadsasfds");
		selenium.type("name=seatID", "F25A");
		selenium.click("//div[@id='page1']/div/label[2]/span");
		// WHY IS THERE NO MCDONALDS AS A CHOICE HERE!!!!!!
		selenium.click("xpath=(//input[@name='mealOption'])[2]");
		// Oh crap, my name is Too long maybe the program is fail-safe.
		// MEANING I DON'T GET AN HTTP ERROR OR A BLANK PAGE!!!!!!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		assertTrue(selenium.isTextPresent("Reservation Failed!"));
	}

	// Created by Steven Chow
	// Scenario 3: Cancelled Reservation
	// Test Case 1: Go back to home page
	@Test
	public void testMakeReservationSC3TC1() throws Exception {
		// Go to website
		selenium.open("");
		selenium.click("css=div.tile.bg-color-red > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Hey Mr. Login!!! I need to use u in order to access the portal to
		// StormWind!!
		selenium.type("name=username", "User1");
		selenium.type("name=password", "pass1");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Portal!! R u there? I must deliver a message to King Varian of a
		// Horde Invasion!!
		assertEquals("Customer Portal", selenium.getTitle());
		// OH CRAP!!! THe portal Ported my to IronForge. I need to book a flight
		selenium.click("css=div.tile-content.icon > img");
		selenium.waitForPageToLoad("30000");
		// NO TIME TO FILTER!! THE ALLIANCE IS IN TROUBLE IF WE Don't WARN THE
		// KING!!
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Let's pick the 1st one. That gryphon is damn sexy!!
		selenium.click("xpath=(//label[@onclick=''])[2]");
		selenium.click("xpath=(//input[@name='r1'])[2]");
		// Let's Reserve that BABY!!!!
		selenium.click("css=div > input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Let's fill in our info
		selenium.type("name=firstName", "test");
		selenium.type("name=lastName", "test");
		selenium.type("name=seatID", "34");
		selenium.click("css=label.radio > span");
		selenium.click("name=mealOption");
		// OH WAIT!! THERE IS A HOT HUMAN MAGE HERE!! She can just Port me to
		// StormWind!!
		// Let's not make a reservation!
		selenium.click("//a[contains(@href, '../Website/index_loggedin.jsp?username=User1')]");
		selenium.waitForPageToLoad("30000");
		// Is the portal made? Man that girl is sooooo nice!! I better ask for
		// her number!! Wait are there phones in WoW?
		assertEquals("Customer Portal", selenium.getTitle());
		// That reservation better be made OR I will pimp slap those dwarves!!
		selenium.click("css=div.tile.bg-color-blue > div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		assertFalse(selenium.isTextPresent("test"));
	}
	
	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}
}
